const express = require("express");
const db = require("../db/index");
const router = express.Router();

router.get("/get", function (req, res) {
  const query = req.query;
  let data = db.apps.get("apps").sortBy("name").sortBy("hasImp").value() || [];
  if (query.appName) {
    data = data.filter((log) => log.appName == query.appName);
  }
  return res.json({
    code: 0,
    data,
  });
});
router.post("/add", function (req, res) {
  const body = req.body;
  db.apps.get("apps").push(body).write();
  return res.json({
    code: 0,
    data: [],
  });
});
router.post("/hasImp", function (req, res) {
  const body = req.body;
  if (!body.name) {
    return res.json({
      code: -1,
      data: [],
      msg: "请输入应用名称",
    });
  }
  db.apps
    .get("apps")
    .find({
      name: body.name,
    })
    .assign({
      hasImp: body.hasImp,
    })
    .write();
  return res.json({
    code: 0,
    data: [],
  });
});

module.exports = router;
